package fast.mvc;

import java.awt.Color;
import java.awt.Dimension;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class BreakdownPanel extends JPanel {
	
	private static final long serialVersionUID = 852930853916977746L;
	double dps = 0.0;
	HashMap<String, Double> map = null;
	TreeMap<String, Double> sorted_map = null;
	
	public BreakdownPanel(Map<String, Double> map){
		this.map = (HashMap<String, Double>) map;
		
		ValueComparator vc = new ValueComparator(map);
		sorted_map = vc.getSortedMap();
		if(map != null){
	        for(String name : map.keySet()){
	        	dps += (Double) map.get(name);
	        }
		}
		this.setLayout(null);
		showItems();
	}
	
	public void showItems() {
		int count = 0;
		for(String name : sorted_map.keySet()){
			JPanel itemContainer = new JPanel(null);
			itemContainer.setBounds(0, count*25, 320, 25);
			this.add(itemContainer);
			if(count % 2 == 0){
				itemContainer.setBackground(Color.decode("#D1D0D0"));
			}else
				itemContainer.setBackground(null);
			
			JLabel labelName = new JLabel(name);
			labelName.setBounds(5, 0, 220, 20);
			itemContainer.add(labelName);
			
			JLabel labelDps = new JLabel(String.format("%.2f %%", map.get(name)/dps*100));
			labelDps.setBounds(145, 0,50,20);
			labelDps.setHorizontalAlignment(JLabel.RIGHT);
			itemContainer.add(labelDps);
			
			JLabel label = new JLabel(String.format("%.2f DPS", map.get(name)));
            label.setBounds(195, 0,120,20);
            label.setHorizontalAlignment(JLabel.RIGHT);
			itemContainer.add(label);
			count++;
		}
		
		JPanel itemContainer = new JPanel(null);
		itemContainer.setBounds(0, count*25, 320, 25);
		this.add(itemContainer);
		if(count % 2 == 0){
			itemContainer.setBackground(Color.decode("#D1D0D0"));
		}else
			itemContainer.setBackground(null);
		
		JLabel labelName = new JLabel("Overall DPS");
		labelName.setBounds(5, 0, 220, 20);
		itemContainer.add(labelName);
		
		JLabel labelDps = new JLabel(String.format("%.2f %%", 100.));
		labelDps.setBounds(145, 0,50,20);
		labelDps.setHorizontalAlignment(JLabel.RIGHT);
		itemContainer.add(labelDps);
		
		JLabel label = new JLabel(String.format("%.2f DPS", dps));
        label.setBounds(195, 0,120,20);
        label.setHorizontalAlignment(JLabel.RIGHT);
		itemContainer.add(label);
		itemContainer.setBorder(BorderFactory.createMatteBorder(
                2, 0, 0, 0, Color.black));
		count++;
		
		this.setPreferredSize(new Dimension(300,1+count*25));
		this.setSize(new Dimension(300,1+count*25));
	}
	
	public class ValueComparator implements Comparator<String> {
		
		private Map<String, Double> map;
		private TreeMap<String, Double> sortedMap;
		
		public ValueComparator(Map<String, Double> map2){
			map = map2;
			sortedMap = new TreeMap<String, Double>(this);
			if(map != null)
				sortedMap.putAll(map);
		}
		
		@Override
		public int compare(String o1, String o2) {
			return map.get(o2).compareTo(map.get(o1));
		}
		public TreeMap<String, Double> getSortedMap(){
			return sortedMap;
		}
	}
}
